﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Core.Extentions;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Domain.Models
{
    /// <summary>
    /// 入库单
    /// </summary>
    public partial class ProductInBill
    {
        public ProductInBill()
        {
            this.ProductInBillDetails= new HashSet<ProductInBillDetail>();
        }
        public ProductInBill Init()
        {
            if (this.Id.Equals(Guid.Empty))
            {
                this.Id = (Guid)new SequentialIdentityGenerator().Next;
            }
            this.CreateTime = DateTime.Now;
            this.Deleted = 0;
            return this;
        }
        /// <summary>
        /// 添加入库单明细行
        /// </summary>
        /// <param name="productInBillDetail"></param>
        /// <returns></returns>
        public ProductInBill AddChild(ProductInBillDetail productInBillDetail)
        {
            if (!this.ProductInBillDetails.Contains(productInBillDetail))
            {
                productInBillDetail.ProductInBillID = this.Id;
                this.ProductInBillDetails.Add(productInBillDetail.Init());//进行初始化
            }
            return this;
        }
        public ProductInBill AddChildren(IEnumerable<ProductInBillDetail> productInBillDetails)
        {
            if (productInBillDetails != null&&productInBillDetails.Count()!=0)
            {
                productInBillDetails.Each(p => AddChild(p));
            }
            return this;
        }
        public ProductInBill ClearChildrenAll()
        {
            if (!this.ProductInBillDetails.Equals(null) && this.ProductInBillDetails.Count() != 0)
            {
                this.ProductInBillDetails.Clear();
            }
            return this;
        }
        /// <summary>
        /// 设置删除状态（若为删除，则置为正常，否则为删除）
        /// </summary>
        /// <returns></returns>
        public ProductInBill SetDeleteState()
        {
            this.Deleted = this.Deleted == 0 ? 1 : 0;
            return this;
        }
        /// <summary>
        ///  设置删除状态（若为删除，则置为正常，否则为删除）
        /// </summary>
        /// <param name="productInBillDetails"></param>
        /// <returns></returns>
        public ProductInBill SetChildrenDeleteState(IEnumerable<ProductInBillDetail> productInBillDetails)
        {
            if (!productInBillDetails.Equals(null) && productInBillDetails.Count() != 0)
            {
                productInBillDetails.Each(p => p.SetDeleteState());
            }
            return this;
        }

    }
}
